package model.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import model.dao.ulti.HibernateUtil;
import model.pojo.KhachHang;
import org.hibernate.Query;
import org.hibernate.Session;

public class KhachHangDAO extends AbstractDAO<KhachHang, String> {

    @Override
    protected Class getPOJOClass() {
        return KhachHang.class;
    }

    public static boolean kiemtraEmail(String email) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from KhachHang kh where kh.email=:kh_email";
        Query query = session.createQuery(hql);
        query.setString("kh_email", email);
        int n = query.list().size();
        session.close();
        if (n > 0) {
            return true;
        }
        return false;
    }

    public static ArrayList<KhachHang> kttaikhoan(String email, String matKhau) {
        ArrayList<KhachHang> ds;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from KhachHang kh where kh.email=:kh_email and kh.matKhau=:kh_matKhau";
        Query query = session.createQuery(hql);
        query.setString("kh_email", email);
        query.setString("kh_matKhau", matKhau);
        ds = (ArrayList<KhachHang>) query.list();
        session.close();
        return ds;
    }

    public static String sinhMaKhachHang() {
        ArrayList<KhachHang> ds;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from KhachHang s order by s.maKhachHang";
        Query query = session.createQuery(hql);
        ds = (ArrayList<KhachHang>) query.list();
        session.close();
        String MaCuoi = "";
        if (ds.size() > 0) {
            MaCuoi = ds.get(ds.size() - 1).getMaKhachHang();
        }
        else{
            MaCuoi="KH000";
        }
        String tam = MaCuoi.substring(2);
        int tam2 = Integer.parseInt(tam);
        tam2++;
        String kq = "KH";
        int n = tam2 / 10 + 1;
        for (int i = 0; i < 3 - n; i++) {
            kq += "0";
        }
        return kq += tam2;
    }

    public static String sinhMaKhachHangabc(Date date) {
        String maKhachHang;
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSSSddMMyyyy");
        String str = sdf.format(date);
        maKhachHang = "KH" + str;
        return maKhachHang;
    }
}
